package com.easy.web.service.impl;

import com.easy.web.mapper.UserLevelMapper;
import com.easy.web.mapper.UserMapper;
import com.easy.web.model.User;
import com.easy.web.model.UserExample;
import com.easy.web.model.UserLevel;
import com.easy.web.model.UserLevelExample;
import com.easy.web.service.UserService;
import com.easy.web.utils.DateUtil;
import com.easy.web.utils.StringUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/11/28.
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {


    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserLevelMapper userLevelMapper;

    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public User queryUserByUserName(String userName) {
        //用户名登陆
        UserExample userExampleName = new UserExample();
        userExampleName.createCriteria().andNameEqualTo(userName);
        List<User> userListName = userMapper.selectByExample(userExampleName);
        if(userListName.size() >0){
            return userListName.get(0);
        }
        //手机号登陆
        UserExample userExamplePhone = new UserExample();
        userExamplePhone.createCriteria().andPhoneEqualTo(userName);
        List<User> userListPhone = userMapper.selectByExample(userExamplePhone);
        if(userListPhone.size() >0){
            return userListPhone.get(0);
        }
        return null;
    }

    /**
     * 根据手机号查询用户信息
     *
     * @param phoneNum
     */
    @Override
    public User queryUserByPhoneNum(String phoneNum) {
        //手机号查询
        UserExample userExamplePhone = new UserExample();
        userExamplePhone.createCriteria().andPhoneEqualTo(phoneNum);
        List<User> userListPhone = userMapper.selectByExample(userExamplePhone);
        if(userListPhone.size() >0){
            return userListPhone.get(0);
        }
        return null;
    }

    /**
     * 修改用户信息
     */
    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 查询所有用户等级
     *
     * @return
     */
    @Override
//    @Cacheable(value = "UserLevel", key = "0")
    public List<UserLevel> queryAllUserLevel() {
        UserLevelExample userLevelExample = new UserLevelExample();
        userLevelExample.setOrderByClause("seq");
        return userLevelMapper.selectByExample(userLevelExample);
    }

    /**
     * 根据用户等级ID查询等级信息
     *
     * @param userLevelId
     * @return
     */
    @Override
//    @Cacheable(value = "UserLevel", key = "#userLevelId")
    public UserLevel queryUserLevelByLevelId(String userLevelId) {
        return userLevelMapper.selectByPrimaryKey(userLevelId);
    }

    /**
     * 显示所有用户
     *
     * @return
     */
    @Override
    public List<User> getUserList() {
        UserExample userExample=new UserExample();
        userExample.setOrderByClause("create_time desc");
        return userMapper.selectByExample(userExample);
    }

    /**
     * 模糊查询用户
     *
     * @param useJson
     */
    @Override
    public List<User> getUserListByLike(JSONObject useJson) {
        UserExample userExample =new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
         if(!useJson.isEmpty()){
            String useName =useJson.optString("name");
            if(StringUtil.isNotEmpty(useName)){
                criteria.andNameLike("%"+useName+"%");
            }
            String phone =useJson.optString("phone");
            if(StringUtil.isNotEmpty(phone)){
                criteria.andPhoneLike("%"+phone+"%");
            }
            String startTime =useJson.optString("startTime");
            if(StringUtil.isNotEmpty(startTime)){
                criteria.andCreateTimeGreaterThanOrEqualTo(DateUtil.parseDatePaf(startTime));
            }
            String endTime =useJson.optString("endTime");
            if(StringUtil.isNotEmpty(endTime)){
                criteria.andCreateTimeLessThanOrEqualTo(DateUtil.parseDatePaf(endTime));
            }
            String qq =useJson.optString("qq");
            if(StringUtil.isNotEmpty(qq)){
                criteria.andQqLike("%"+qq+"%");
            }
            String email =useJson.optString("email");
            if(StringUtil.isNotEmpty(email)){
                criteria.andEmailLike("%"+email+"%");
            }

            String level =useJson.optString("level");
            if(StringUtil.isNotEmpty(level)){
                UserLevel userlevel=getLevelInfo(level);
                if(userlevel!=null){
                    criteria.andLevelEqualTo(userlevel.getId());
                }else {
                    logger.info("当前请求等级异常，参数"+useJson);
                }

            }
            String isAuditor =useJson.optString("isAuditor");
            if(StringUtil.isNotEmpty(isAuditor)){
                criteria.andIsAuditorEqualTo(isAuditor);
            }


        }

        userExample.setOrderByClause("create_time desc");
        List<User> userList=userMapper.selectByExample(userExample);
        return userList;
    }

    /**
     * 删除用户信息
     *
     * @param id
     */
    @Override
    public void delUser(String id) {
        userMapper.deleteByPrimaryKey(id);
    }


    /**
     * 根据id 查询某个用户
     *
     * @param id
     */
    @Override
    public User queryUserById(String id) {

        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    /**
     * 根据等级名称查询等级信息
     *
     * @param name
     */
    @Override
    public UserLevel getLevelInfo(String name) {
        UserLevelExample userLevelExample = new UserLevelExample();
        userLevelExample.createCriteria().andNameEqualTo(name);
        List<UserLevel> userLevels=userLevelMapper.selectByExample(userLevelExample);
        if(userLevels.size()>0){
            return userLevels.get(0);
        }
        return null;
    }
}
